﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Runtime.InteropServices;
using System.Drawing;
using Microsoft.Win32;
namespace FFWinMobile
{
    public static class GF
    {
        static GF()
        {
            string path = Registry.CurrentUser + "\\Software\\FFWinMobile\\";
            if (LocalSettings.loadBool("InitialSettingsSaved") == null || LocalSettings.loadBool("InitialSettingsSaved") == false)
            {
                GF.RememberMe = false;
                GF.ShowPics = false;
                GF.PageRecordCount = 10;
                LocalSettings.save("InitialSettingsSaved", true);
            }          

        }
        public static bool giris;
        public static bool Giris
        {
            get
            {
                if (!giris)
                {
                    if (RememberMe)
                    {   
                        bool cevap = FF.Validate();
                        if (cevap)
                        {
                            giris = true;                            
                        }
                    }
                }

                return giris;
            }
            set{

                giris = value;
            }
        }
        public static string UserName
        {
            get
            {
                return LocalSettings.loadString("username");
            }
            set
            {
                LocalSettings.save("username", value);
            }
        }
        public static string Password
        {
            get
            {
                return LocalSettings.loadString("password");
            }
            set
            {
                LocalSettings.save("password", value);
            }
        }
        public static string OAuthToken
        {
            get
            {
                return LocalSettings.loadString("oauth_token");
            }
            set
            {
                LocalSettings.save("oauth_token", value);
            }
        }
        public static string OAuthTokenSecret
        {
            get
            {
                return LocalSettings.loadString("oauth_token_secret");
            }
            set
            {
                LocalSettings.save("oauth_token_secret", value);
            }
        }
        public static int PageRecordCount
        {
            get
            {
                return LocalSettings.loadInt("PageRecordCount");
            }
            set
            {
                LocalSettings.save("PageRecordCount", value);
            }
        }
        public static bool RememberMe
        {
            get
            {
                return LocalSettings.loadBool("RememberMe");
            }
            set
            {
                LocalSettings.save("RememberMe", value);
            }
        }
        public static bool ShowPics
        {
            get
            {
                return LocalSettings.loadBool("ShowPics");
            }
            set
            {
                LocalSettings.save("ShowPics", value);
            }
        }

        public static Image DownloadImage(string url)
        {
            HttpWebRequest r = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse res = (HttpWebResponse)r.GetResponse();
            Image img = new Bitmap(res.GetResponseStream());
            res.Close();
            return img;

        }
       
    }
    public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
    {
        public TrustAllCertificatePolicy()
        { }

        public bool CheckValidationResult(ServicePoint sp,
         X509Certificate cert, WebRequest req, int problem)
        {
            return true;
        }
    }
    public static class LocalSettings
    {
        private static String PATH = Registry.CurrentUser + "\\Software\\FFWinMobile\\";

        public static void save(String valuename, object value)
        {
            Registry.SetValue(PATH, valuename, value);
        }
        public static string loadString(String valuename)
        {
            object obj = Registry.GetValue(PATH, valuename, null);
            if (obj == null)
            {
                return "";
            }
            else
            {
                return (string)obj;
            }
        }
        public static long loadDateTime(String valuename)
        {
            object obj = Registry.GetValue(PATH, valuename, null);
            if (obj == null)
            {
                return System.DateTime.Now.Ticks;
            }
            else
            {
                return long.Parse((string)obj);
            }
        }
        public static bool loadBool(String valuename)
        {
            object obj = Registry.GetValue(PATH, valuename, null);
            if (obj == null)
            {
                return false;
            }
            else
            {
                return bool.Parse((string)obj);
            }
        }
        public static int loadInt(String valuename)
        {
            object obj = Registry.GetValue(PATH, valuename, null);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return (int)obj;
            }
        }
    }  

}
